﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


 class SerializeStudent
 {
     class Student
     {
         public string Name {get; set;}
         public string Lastname{get; set;}
         public int FacNumber{ get; set;}

         public Student(string n, string l, int num)
         {
             this.Name = n;
             this.Lastname = l;
             this.FacNumber = num;
         }
     }

        static void AddStudent(Student s)
        {
            FileStream f = new FileStream("students.bin", FileMode.Append);

            using (var fs = new BinaryWriter(f))
            {
                string fullName = s.Name + s.Lastname;     
                fs.Write(fullName );
                fs.Write(s.FacNumber);
            }
           
        }

        static void PrintAll()
        {
            using (var reader = new BinaryReader(File.Open("students.bin", FileMode.Open)))
            {
                while(reader.PeekChar() != -1)
                {
                    Console.WriteLine(reader.ReadString());
                    Console.WriteLine(reader.ReadInt32());
                }
            }
        }

   
        static void DeleteStudent(int facNum)
        {
            FileStream fs = new FileStream("students.bin", FileMode.Open);
            BinaryReader reader = new BinaryReader(fs);

            BinaryWriter writer = new BinaryWriter(File.Create("temp.bin"));

            using (writer)
            {
                using (reader)
                {
                    while (reader.PeekChar() != -1)
                    {
                        string name = reader.ReadString();
                        int fnum = reader.ReadInt32();

                        if (fnum != facNum)
                        {
                            writer.Write(name);
                            writer.Write(facNum);
                        }

                    }

                }
            }
            File.Copy("temp.bin", "students.bin", true);
        }


        static void Main()
        {
           // DeleteStudent(80145);
           // PrintAll();

        }
 }

